import storage from '@/pages_temp/utils/storage.js'

function test() {
	console.log('123');
}

function toQueryString(obj) {
	if (obj instanceof Object) {
		let tmp = []
		Object.keys(obj).forEach(key => {
			tmp.push(key + '=' + obj[key])
		})
		return tmp.join('&')
	} else {
		return ''
	}
}

function getToken() {
	return storage.get('token')
}

function setToken(token) {
	storage.set('token', token)
	console.log();
}

function login() {
	return new Promise((resolve, reject) => {
		if (getToken()) {
			uni.checkSession().then(() => {
				console.log('checkSession: success !!')
				resolve()
			}).catch(() => {
				uni.login({
					provider: 'weixin'
				}).then(res => {
					console.log('uni.login: ', res);
					resolve(res)
				}).catch(() => {
					console.log('uni.login: fail')
					reject()
				})
			})
		} else {
			uni.login({
				provider: 'weixin'
			}).then(res => {
				console.log('uni.login: ', res);
				resolve(res)
			}).catch(() => {
				console.log('uni.login: fail')
				reject()
			})
		}

	})
}

export default {
	test,
	toQueryString,
	getToken,
	setToken,
	login,
}
